package com.etone.system.service.impl;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONObject;
import com.etone.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.etone.system.mapper.SysDibiaoMapper;
import com.etone.system.domain.SysDibiao;
import com.etone.system.service.ISysDibiaoService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author etone
 * @date 2025-05-27
 */
@Service
public class SysDibiaoServiceImpl implements ISysDibiaoService
{
    @Autowired
    private SysDibiaoMapper sysDibiaoMapper;
    @Autowired
    private AgvServiceImpl agvService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysDibiao selectSysDibiaoById(Long id)
    {
        return sysDibiaoMapper.selectSysDibiaoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysDibiao 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysDibiao> selectSysDibiaoList(SysDibiao sysDibiao)
    {
        return sysDibiaoMapper.selectSysDibiaoList(sysDibiao);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysDibiao 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysDibiao(SysDibiao sysDibiao)
    {
        sysDibiao.setCreateTime(DateUtils.getNowDate());
        return sysDibiaoMapper.insertSysDibiao(sysDibiao);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysDibiao 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysDibiao(SysDibiao sysDibiao)
    {
        sysDibiao.setUpdateTime(DateUtils.getNowDate());
        return sysDibiaoMapper.updateSysDibiao(sysDibiao);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysDibiaoByIds(Long[] ids)
    {
        return sysDibiaoMapper.deleteSysDibiaoByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysDibiaoById(Long id)
    {
        return sysDibiaoMapper.deleteSysDibiaoById(id);
    }

    @Override
    public List<SysDibiao> getApplist() {
        List<SysDibiao> list = sysDibiaoMapper.getApplist();
        return list;
    }

    @Override
    public List<Map<String, Object>> getLieBiaoList() {
        List<Map<String, Object>> list = sysDibiaoMapper.getLieBiaoList();
        return list;
    }

    @Override
    public int addByHuojia(String name,String type, String huojia) {
//        SysDibiao sysDibiao = sysDibiaoMapper.selectSysDibiaoById(id);
        int i =0;
        if(huojia!=null){
            if(!huojia.equals("")){
                String result = agvService.bangding(huojia, name, 1);
                JSONObject json = JSONObject.parseObject(result);
                if(json.get("code").toString().equals("0")){
                    SysDibiao sysDibiao = new SysDibiao();
                    sysDibiao.setType(Long.parseLong(type));
                    sysDibiao.setHuojia(huojia);
                    sysDibiao.setName(name);
                    sysDibiao.setIsFree("1");
                    i = sysDibiaoMapper.insertSysDibiao(sysDibiao);
                }
            }else{
                SysDibiao sysDibiao = new SysDibiao();
                sysDibiao.setType(Long.parseLong(type));
                sysDibiao.setHuojia(huojia);
                sysDibiao.setName(name);
                i = sysDibiaoMapper.insertSysDibiao(sysDibiao);
            }
        }else{
            SysDibiao sysDibiao = new SysDibiao();
            sysDibiao.setType(Long.parseLong(type));
            sysDibiao.setHuojia(huojia);
            sysDibiao.setName(name);
            i = sysDibiaoMapper.insertSysDibiao(sysDibiao);
        }
        return i;
    }

    @Override
    public int jiebangByHuojia(Long id) {
        SysDibiao sysDibiao = sysDibiaoMapper.selectSysDibiaoById(id);
        int i =0;
        if(sysDibiao.getHuojia()!=null){
            String result = agvService.bangding(sysDibiao.getHuojia(), sysDibiao.getName(), 0);
            JSONObject json = JSONObject.parseObject(result);
            if(json.get("code").toString().equals("0")){
                i = sysDibiaoMapper.jiebangByHuojia(id);
            }
        }else{
            i = sysDibiaoMapper.jiebangByHuojia(id);
        }
        return i;
    }

    @Override
    public List<SysDibiao> selectSysFreeDibiaoList(SysDibiao sysDibiao) {
        return sysDibiaoMapper.selectSysFreeDibiaoList(sysDibiao);
    }
}
